package y2016.date1014.pm.字符串效率比较;

import org.junit.jupiter.api.Test;

public class CompareString {

    @Test
    public void main() throws Exception {
        int num = 1000;//十万次
//		buildString(num);
        for (int i = 0; i < 10; i++) {
            buildString(num);
//			buildStringBuffer(num);
//			buildStringBuilder(num);
//			System.out.println();
        }
//		buildStringBuilder(num);

    }

    public static void buildString(int num) {
        String a = "ab";
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            a += i + "";
        }
        System.out.println("String耗时：" + (System.currentTimeMillis() - time1) + "毫秒");
    }

    public static void buildStringBuilder(int num) {
        StringBuilder a = new StringBuilder("ab");
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            a.append(i + "");
        }
        System.out.println("StringBuilder耗时：" + (System.currentTimeMillis() - time1) + "毫秒");
    }

    public static void buildStringBuffer(int num) {
        StringBuffer a = new StringBuffer("ab");
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            a.append(i + "");
        }
        System.out.println("StringBuffer耗时：" + (System.currentTimeMillis() - time1) + "毫秒");
    }
}

